package com.thrid.party.codec.demo;


import java.math.BigDecimal;
import java.text.DateFormat;
import java.util.Date;

public class Utilty {

    private static Utilty instance = new Utilty();

    public static Utilty getInstance() {
        return instance;
    }

    public static final int MIN_MID_VALUE = 1;

    public static final int MAX_MID_VALUE = 65535;


    //�ֽ���תΪ�޷�������
    public int bytes2Int(byte[] b, int start, int length) {
        int sum = 0;
        int end = start + length;
        for (int k = start; k < end; k++) {
            int n = ((int) b[k]) & 0xff;
            n <<= (--length) * 8;
            sum += n;
        }
        return sum;
    }

    //����תΪ�ֽ���
    public byte[] int2Bytes(int value, int length) {
        byte[] b = new byte[length];
        for (int k = 0; k < length; k++) {
            b[length - k - 1] = (byte) ((value >> 8 * k) & 0xff);
        }
        return b;
    }

    //�ж�mid�Ƿ���Ч
    public boolean isValidofMid(int mId) {
        if (mId < MIN_MID_VALUE || mId > MAX_MID_VALUE) {
            return false;
        }
        return true;
    }

    /***
     * ���ֽ���ת��Ϊ16�����ַ���
     */
    public static String parseByte2HexStr(byte[] buf) {
        if (null == buf) {
            return null;
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    public String bytes2HexStr(byte[] b, int start, int length) {
        if (null == b) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = start; i < length + start; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    public static float caculateValue(int symbol,int value) {
        float result = 0.000000f;
        if (symbol < 0) {
            result = (float)value/(int) Math.pow(10, 256 + symbol - 128);
//            return result;
            BigDecimal bd = new BigDecimal((double) result);
            bd = bd.setScale(256 + symbol - 128, 4);
            result = bd.floatValue();

        } else if (symbol > 0){
            result = value/(int) Math.pow(10, symbol);
            BigDecimal bd = new BigDecimal((double) result);
            bd = bd.setScale(symbol, 4);
            result = - bd.floatValue();

        } else if (symbol == 0) {
            result = value/(int) Math.pow(10, symbol);
            BigDecimal bd = new BigDecimal((double) result);
            bd = bd.setScale(symbol, 4);
            result = bd.floatValue();

        }
        return result;
    }

    public byte[] time2Bytes (String time, int length) {
        byte[] b = new byte[length];
        b[0] = (byte) Integer.parseInt(time.substring(0,2));
        b[1] = (byte) Integer.parseInt(time.substring(2,4));
        b[2] = (byte) Integer.parseInt(time.substring(4,6));
        b[3] = (byte) Integer.parseInt(time.substring(6,8));
        b[4] = (byte) Integer.parseInt(time.substring(8,10));
        b[5] = (byte) Integer.parseInt(time.substring(10,12));
        return b;
    }

    public byte[] float2Bytes (float f, int length) {
        byte[] b = new byte[length];
        if (f >= 0) {
            b[0] = (byte) (128 + (f + "").length() - (f + "").indexOf(".") + 1);
        } else {
            b[0] = (byte) ((f + "").length() - (f + "").indexOf(".") + 1);
        }
        int a = (int) (f * ((int) Math.pow(10, (f + "").length() - (f + "").indexOf(".") + 1)));
        byte[] bytes = int2Bytes(a, 2);
        b[1] = bytes[0];
        b[2] = bytes[1];
        return b;
    }

    public byte[] str2Bytes(String s) {
        byte[] b = new byte[s.length()];
        for (int i = 0; i < s.length(); i++) {
            b[i] = (byte) (Integer.parseInt(s.substring(i, i+1)) & 0xFF);
        }
        return b;
    }

    public byte getCs(byte[] b, int start, int length) {
        byte by = 0x00;
        for (int i = start; i < length + start; i++) {
            by = (byte) (((byte) (by + b[i])) & 0x03);
        }
        return by;
    }
}
